package 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	[SWF(width = "1000", height = "550", backgroundColor = "#000000", frameRate="60")]
	
	/**
	 * ...
	 * @author UWGameDevClub
	 */
	public class Main extends Sprite 
	{
		public static var main:Main;
		
		public var paused:Boolean = false;
		
		//Framerate
		public static const FRAME_RATE:Number = 60;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public var rooms:Rooms;
		private var room:Room;
		private var d:Dialog = new Dialog( stage.stageWidth, 100 );
		public var timer:Time;
		public var music:Music;
		
		public function setRoom( r:Room ):void
		{
			if ( room != null ) {
				room.shutdown();
				removeChild(room);
				room = null;
			}
			room = r;
			addChild(r);
			room.startup();
			
			//make sure the room's at the bottom, so that the dialog and timer are drawn on top
			setChildIndex(r, 0);
		}
		
		private var _dad:Item;
		private var _receipt:FrameItem;
		private var _receiptShown:Boolean = false;
		
		public function showDad():void {
			room.add(_dad);
			//center dad
			_dad.x = (stage.stageWidth / 2) - (_dad.width / 2) - room.x;
			_dad.y = -room.y;
		}
		
		public function hideDad():void {
			room.remove(_dad);
		}
		
		public function showReceipt(boozeFound:Number):void {
			switch(boozeFound) {
				case 0x000: _receipt.frame(0); break;
				case 0x001: _receipt.frame(1); break;
				case 0x010: _receipt.frame(2); break;
				case 0x011: _receipt.frame(3); break;
				case 0x100: _receipt.frame(4); break;
				case 0x101: _receipt.frame(5); break;
				case 0x110: _receipt.frame(6); break;
				case 0x111: _receipt.frame(7); break;
			}
			room.add(_receipt);
			_receiptShown = true;
			//center the receipt
			_receipt.x = (stage.stageWidth / 2) - (_receipt.width / 2) - room.x;
			_receipt.y = stage.stageHeight - _receipt.height - room.y;
		}
		
		public function hideReceipt():void {
			if (_receiptShown) {
				room.remove(_receipt);
				_receiptShown = false;
			}
		}
		
		private var _arrow:FrameItem;
		
		public function showArrow(direction:Number):void {
			Mouse.hide();
			_arrow.frame(direction);
			_arrow.visible = true;
			
			_arrow.x = mouseX - _arrow.width / 2;
			_arrow.y = mouseY - _arrow.height / 2;
		}
		
		public function hideArrow():void {
			Mouse.show();
			_arrow.visible = false;
		}
		
		private function init(e:Event = null):void 
		{
			main = this;
			
			rooms = new Rooms();
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			//Create a new room
			setRoom( rooms.Bedroom );
			
			//Create the dialog box
			d.x = 0; d.y = stage.stageHeight - 100;
			d.visible = false;
			addEventListener( MouseEvent.CLICK, nextLine );
			addChild(d);
			
			//create the timer
			timer = new Time(stage.stageWidth - 100, 25, 50, 25);
			addChild(timer);
			
			_dad = new Item(Art.Dad, Items.DAD);
			_dad.mouseEnabled = false;
			_receipt = new FrameItem( [ Art.BoozeReceipt000, Art.BoozeReceipt001, Art.BoozeReceipt010, Art.BoozeReceipt011,
										Art.BoozeReceipt100, Art.BoozeReceipt101, Art.BoozeReceipt110, Art.BoozeReceipt111],
										Items.BOOZERECEIPT);
			_receipt.mouseEnabled = false;
										
			_arrow = new FrameItem( [ Art.ArrowLeft, Art.ArrowRight ], Items.MOVEARROW);
			_arrow.visible = false;
			_arrow.mouseEnabled = false;
			addChild(_arrow);
			
			//get that music going
			music = new Music();
			music.start();
			
			playScript( Scripts.Intro );
		}
		
		private var script:Array = new Array();
		private var index:Number = 0;
		
		//Plays out a script
		public function playScript( s:Array ):void
		{
			paused = true;
			d.visible = true;
			index = 0;
			script = s;
			if (s[index] is Function) {
				s[index]();
				nextLine(null);
			}
			else d.post( s[0][0], s[0][1] );
		}
		
		private function nextLine( e:MouseEvent ):void
		{
			if (_end) return;
			
			if ( !d.finished() ) {
				d.finish();
				return;
			}
			if ( index < script.length - 1 )
			{
				index++;
				if (script[index] is Function) {
					script[index]();
					nextLine(null);
				}
				else d.post( script[index][0], script[index][1] );
			}
			else
			{
				paused = false;
				d.visible = false;
				index = 0;
				if( e != null && e.target is Item )
					(e.target as Item).dispatchEvent( e );
			}
		}
		
		private var _end:Boolean = false;
		
		public function theEnd():void {
			_end = true;
		}
	}
	
}